package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Operation;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.service.ApprovalOperateServicePrx;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/operateRequest")
public class OperateRequest extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(OperateRequest.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            String reqBody = getContent(req);
            Request request = JSONObject.parseObject(reqBody, Request.class);

            check(request);
            request.setUser(user);

            ApprovalOperateServicePrx approvalOperateServicePrx = ServantUtils.getObject(ApprovalOperateServicePrx.class);
            switch (request.getOperate()) {
                case Operation.CONFIRM:
                case Operation.FINANCIAL_CONFIRM:
                    logger.info("用户{}批准审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.confirmRequest(request.getRequestDkey(), user);
                    break;
                case Operation.REFUSE:
                case Operation.FINANCIAL_REFUSE:
                    logger.info("用户{}驳回审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.refuseRequest(request.getRequestDkey(), user);
                    break;
                case Operation.ARCHIVE:
                    logger.info("用户{}归档审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.fileRequest(request);
                    break;
                case Operation.CANCEL:
                    logger.info("用户{}作废审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.cancelRequest(request);
                    break;
                case Operation.DELETE:
                    logger.info("用户{}删除审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.deleteRequest(request);
                    break;
                case Operation.PAUSE_LEAVE:
                case Operation.PAUSE_OUTDOOR:
                case Operation.PAUSE_BUSINESS_TRIP:
                    logger.info("用户{}暂停审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.pauseRequest(request);
                    break;
                case Operation.WITHDRAW_APPLY:
                    logger.info("用户{}撤销审批:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.withdrawApply(request);
                    break;
                case Operation.WITHDRAW_APPROVE:
                    logger.info("用户{}撤销批准:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.withdrawApprove(request);
                    break;
                case Operation.WITHDRAW_ARCHIVE:
                    logger.info("用户{}撤销归档:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.withdrawFiled(request);
                    break;
                case Operation.WITHDRAW_PAUSE_LEAVE:
                    logger.info("用户{}撤销销假:{}", user.getCompanyInfoId(), reqBody);
                    approvalOperateServicePrx.cancelPauseRequest(request);
                    break;
                case Operation.ARCHIVE_ALL:
                    logger.info("用户{}批量归档审批:{}", user.getCompanyInfoId(), reqBody);
                    request.setCompanyId(user.getCompanyId());
                    request.setCompanyInfoId(user.getCompanyInfoId());
                    approvalOperateServicePrx.archiveAllRequest(request);
                    break;
                default:
                    logger.info("操作类型:{}", JSONObject.toJSONString(request));
                    break;
            }

            printSuccessJson(resp);
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private void check(Request request) {
        if (Utils.isNull(request.getOperate())) {
            throw new RpcServerLogicalException(1, "操作类型不能为空");
        }
    }

}
